﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EEPManager
{
    public partial class frmGroupListByUser : Form
    {
        string _UserId;

        public frmGroupListByUser(string UserId, string UserName)
        {
            InitializeComponent();
            this.Text = UserId + " - " + UserName;
            _UserId = UserId;

            DataSet Uds = UserGroup(UserId);
            DataSet Ads = AllGroup();

            //fill list1
            if (Uds != null && Uds.Tables[0] != null)
            {

                foreach (DataRow dr in Uds.Tables[0].Rows)
                {
                    ListItem item = new ListItem(dr["GroupId"].ToString(),
                    dr["GroupId"].ToString() + " - " + dr["GroupName"].ToString() + "[" + dr["DESCRIPTION"].ToString() + "]");


                    listBox1.Items.Add(item);
                }
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "ID";
            }


            if (Ads != null && Ads.Tables[0] != null)
            {

                foreach (DataRow dr in Ads.Tables[0].Rows)
                {
                    ListItem item = new ListItem(dr["GroupId"].ToString(),
                        dr["GroupId"].ToString() + " - " + dr["GroupName"].ToString() + "[" + dr["DESCRIPTION"].ToString() + "]");

                    listBox2.Items.Add(item);
                }
                listBox2.DisplayMember = "Name";
                listBox2.ValueMember = "ID";
            }
        }

        public DataSet UserGroup(string UserId)
        {
            string Sql = "select USERGROUPS.*, USERS.USERNAME,GROUPS.GROUPName,GROUPS.DESCRIPTION from USERS " +
               " inner join USERGROUPS on USERGROUPS.USERID = USERS.USERID " +
               " inner join GROUPS on GROUPS.groupid=USERGROUPS.groupid " +
               " Where USERS.USERId='" + UserId + "'" +
               " order by USERS.USERId asc";

            return Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse",
                       Sql, true, Srvtools.CliUtils.fCurrentProject);
        }

        public DataSet AllGroup()
        {
            return Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse",
                       "select GROUPId,GROUPName,DESCRIPTION from GROUPS", true, Srvtools.CliUtils.fCurrentProject);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (object o2 in listBox2.SelectedItems)
            {
                ListItem li2 = o2 as ListItem;
                bool flag = false;

                foreach (object o1 in listBox1.Items)
                {
                    ListItem li1 = o1 as ListItem;
                    if (li2.ID == li1.ID)
                    {
                        flag = true;
                        break;
                    }
                }

                if (!flag)
                {
                    listBox1.Items.Add(o2);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            for (int i = this.listBox1.Items.Count - 1; i >= 0; i--)
            {
                if (this.listBox1.SelectedIndices.Contains(i))
                {
                    this.listBox1.Items.RemoveAt(i);
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string Sql = "";
                Sql += "Delete From USERGROUPS Where USERID='" + _UserId + "' \r";
                foreach (object o1 in listBox1.Items)
                {
                    ListItem li1 = o1 as ListItem;
                    Sql += "Insert Into USERGROUPS Values('" + _UserId + "','" + li1.ID + "') \r";
                }

                Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse",
                       Sql, true, Srvtools.CliUtils.fCurrentProject);

                MessageBox.Show("succeed!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    } 

    //<summary>
    //选择项类，用于ComboBox或者ListBox添加项
    //</summary>
    public class ListItem
    {
        private string id = string.Empty;
        private string name = string.Empty;

        public ListItem(string sid, string sname)
        {
            id = sid;
            name = sname;
        }

        public override string ToString()
        {
            return this.name;
        }

        public string ID
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
    }
}
